﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VCN.App.DataAccessLayer;
using VCN.App.DataTransferObjects;

namespace VCN.admin
{
    public partial class RelativeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lbl_Error.Visible = false;
            m_lbl_mess.Visible = false;
            btnUp.Enabled = false;
            if (Page.IsPostBack == false)
            {
                if (lbl_trang_thai_update.Text == "true")
                {
                    btnAdd.Enabled = false;
                    btnUp.Enabled = true;
                }
                else btnAdd.Enabled = true;
                LoadData();
                LoadDataToDdl();
                LoadDataToDdlProduct();
                LoadDataToDdlLoc();
                LoadDataToDdlProductLoc();
                // ReloadUserData();
                Page.Title = "Danh sách sản phẩm liên quan";
            }

        }
        private void LoadData()
        {
            RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
            DataTable v_dt_RelativeProduct = new DataTable();
            v_dt_RelativeProduct = v_RelativeProductDAO.selectAll2Dt();
            if (v_dt_RelativeProduct.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Chưa có dữ liệu !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            dgv_RelativeProduct.DataSource = v_dt_RelativeProduct;
            dgv_RelativeProduct.DataBind();
        }
        private void LoadDataToDdlLoc()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_Category.DataSource = v_dt;
            ddl_Category.DataTextField = "CATEGORY_NAME";
            ddl_Category.DataValueField = "Id";
            ddl_Category.DataBind();
        }
        private void LoadDataToDdlProductLoc()
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt = v_ProductDAO.SelectByCategoryId(int.Parse(ddl_Category.SelectedValue));
            ddl_Product.DataSource = v_dt;
            ddl_Product.DataTextField = "PRODUCT_NAME";
            ddl_Product.DataValueField = "Id";
            ddl_Product.DataBind();
        }
        private void LoadDataToDdl()
        {
            CategoriesDAO v_CategoryDAO = new CategoriesDAO();
            DataTable v_dt = v_CategoryDAO.selectAll2Dt();
            ddl_CategoryName.DataSource = v_dt;
            ddl_CategoryName.DataTextField = "CATEGORY_NAME";
            ddl_CategoryName.DataValueField = "Id";
            ddl_CategoryName.DataBind();
        }
        private void LoadDataToDdlProduct()
        {
            ProductDAO v_ProductDAO = new ProductDAO();
            DataTable v_dt = v_ProductDAO.SelectByCategoryId(int.Parse(ddl_CategoryName.SelectedValue));
            ddl_ProductName.DataSource = v_dt;
            ddl_ProductName.DataTextField = "PRODUCT_NAME";
            ddl_ProductName.DataValueField = "Id";
            ddl_ProductName.DataBind();
        }
        private void ReloadUserData()
        {
            //   string m_id = Request.QueryString["Id"].ToString();
            if (!String.IsNullOrEmpty(lbl_Id.Text))
            {
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                RelativeProduct v_RelativeProduct = new RelativeProduct();
                ProductDAO v_ProductDAO = new ProductDAO();
                v_RelativeProduct = v_RelativeProductDAO.SelectById(int.Parse(lbl_Id.Text));
                imgPreview.AlternateText = v_RelativeProduct.Image;
                m_txt_RelativeProductName.Text = v_RelativeProduct.Name;
                imgPreview.ImageUrl = string.Format("../Images/Product/{0}", v_RelativeProduct.Image);
                try
                {
                    ddl_CategoryName.SelectedValue = v_ProductDAO.SelectById(v_RelativeProduct.ProductId).CateId.ToString();
                    LoadDataToDdlProduct();
                    ddl_ProductName.SelectedValue = v_RelativeProduct.ProductId.ToString();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }

        protected void gridRelativeImage_Delete(object sender, GridViewDeleteEventArgs e)
        {
            RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
            VCN.App.DataTransferObjects.RelativeProduct v_RelativeProduct = new VCN.App.DataTransferObjects.RelativeProduct();
            v_RelativeProduct.Id = int.Parse(dgv_RelativeProduct.DataKeys[e.RowIndex].Value.ToString());
            v_RelativeProductDAO.delete(v_RelativeProduct);
            this.LoadData();
        }

        protected void gridRelativeImage_Edit(object sender, GridViewEditEventArgs e)
        {
            lbl_Id.Text = dgv_RelativeProduct.DataKeys[e.NewEditIndex].Value.ToString();
            ReloadUserData();

            lbl_trang_thai_update.Text = "true";
            // Response.Redirect(string.Format("RelativeProductManagement.aspx?Id={0}", m_id));
        }

        private void ddl_CategoryLoc()
        {
            LoadDataToDdlProductLoc();
            if (ddl_Product.SelectedValue == "") { dgv_RelativeProduct.DataSource = null; dgv_RelativeProduct.DataBind(); }
            else
            {
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                DataTable v_dt_RelativeProduct = new DataTable();
                v_dt_RelativeProduct = v_RelativeProductDAO.searchRelativeProduct(int.Parse(ddl_Category.SelectedValue), int.Parse(ddl_Product.SelectedValue));
                if (v_dt_RelativeProduct.Rows.Count == 0)
                {
                    m_lbl_mess.Text = "Không có kết quả nào liên quan đến từ khóa trên !";
                    m_lbl_mess.Visible = true;
                }
                else
                {
                    m_lbl_mess.Visible = false;
                }
                dgv_RelativeProduct.DataSource = v_dt_RelativeProduct;
                dgv_RelativeProduct.DataBind();
            }

        }
        private void ddl_ProductLoc()
        {
            RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
            DataTable v_dt_RelativeProduct = new DataTable();
            v_dt_RelativeProduct = v_RelativeProductDAO.searchRelativeProduct(int.Parse(ddl_Category.SelectedValue), int.Parse(ddl_Product.SelectedValue));
            if (v_dt_RelativeProduct.Rows.Count == 0)
            {
                m_lbl_mess.Text = "Không có kết quả nào liên quan đến từ khóa trên !";
                m_lbl_mess.Visible = true;
            }
            else
            {
                m_lbl_mess.Visible = false;
            }
            dgv_RelativeProduct.DataSource = v_dt_RelativeProduct;
            dgv_RelativeProduct.DataBind();

        }
        protected void ddl_CategoryLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddl_CategoryLoc();
        }

        protected void ddl_ProductLoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddl_ProductLoc();
        }


        protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataToDdlProduct();
            ddl_Category.SelectedValue = ddl_CategoryName.SelectedValue;
            ddl_CategoryLoc();
        }
        protected void btnAdd_click(object sender, EventArgs e)
        {
            if (lbl_trang_thai_update.Text == "true") return;
            if (FileUpload.HasFile)
            {
                lbl_Error.Visible = false;
                RelativeProduct v_RelativeProduct = new RelativeProduct();
                v_RelativeProduct.ProductId = int.Parse(ddl_ProductName.SelectedItem.Value);
                v_RelativeProduct.Image = this.doUploadPImage();
                v_RelativeProduct.Name = m_txt_RelativeProductName.Text;
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                string somescript;
                if (v_RelativeProductDAO.insert(v_RelativeProduct))
                {

                    somescript = "<script language='javascript'>alert('Thêm mới thành công')</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "succeed", somescript);
                    XoaTrang();
                    LoadData();
                }
                else
                {
                    somescript = "<script language='javascript'>alert('Lỗi thêm sản phẩm liên quan')</script>";
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "succeed", somescript);
                }
            }
            else lbl_Error.Visible = true;


            // Response.Redirect("RelativeList.aspx");
        }

        protected void btnUpdate_click(object sender, EventArgs e)
        {

            if (lbl_trang_thai_update.Text == "false" || lbl_Id.Text == "") return;
            RelativeProduct v_RelativeProduct = new RelativeProduct();
            v_RelativeProduct.Id = int.Parse(lbl_Id.Text);
            v_RelativeProduct.ProductId = int.Parse(ddl_ProductName.SelectedItem.Value);
            v_RelativeProduct.Image = this.doUploadPImage();
            v_RelativeProduct.Name = m_txt_RelativeProductName.Text;
            RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
            string somescript;
            if (v_RelativeProductDAO.update(v_RelativeProduct))
            {
                somescript = "<script language='javascript'>alert('Cập nhật thành công')</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "succeed", somescript);
                XoaTrang();
                lbl_Id.Text = "";
                LoadData();
            }
            else
            {
                somescript = "<script language='javascript'>alert('Lỗi cập nhật sản phẩm liên quan')</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "succeed", somescript);
            }
            lbl_trang_thai_update.Text = "false";
        }
        private void XoaTrang()
        {
            m_txt_RelativeProductName.Text = "";
            imgPreview.AlternateText = "";
            imgPreview.ImageUrl = string.Format("../admin/img/no_images.jpg");
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            XoaTrang();
        }

        private string doUploadPImage()
        {
            if (FileUpload.HasFile)
            {
                var v_str_fstorage = string.Format("../Images/Product/{0}", FileUpload.FileName);
                this.FileUpload.SaveAs(Server.MapPath(v_str_fstorage));
                return FileUpload.FileName;
            }
            return this.imgPreview.AlternateText;
        }

        protected void dgv_RelativeProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                dgv_RelativeProduct.PageIndex = e.NewPageIndex;
                RelativeProductDAO v_RelativeProductDAO = new RelativeProductDAO();
                dgv_RelativeProduct.DataSource = v_RelativeProductDAO.selectAll2Dt();
                dgv_RelativeProduct.DataBind();
            }
            catch (Exception ip_e)
            {
                ip_e.ToString();
            }
        }
        public string GetProductName(object ip_int_Product)
        {
            // string ProductName = "";
            ProductDAO v_ProductDAO = new ProductDAO();
            return v_ProductDAO.SelectById(int.Parse(ip_int_Product.ToString())).Name;
        }

        protected void ddl_ProductName_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddl_Product.SelectedValue = ddl_ProductName.SelectedValue;
            ddl_ProductLoc();
        }
    }
}
